#include "packet.h"
#include "leds.h"

#define MAX_LEDS 9

unsigned char leds[MAX_LEDS];
unsigned char ledcnt = 0;

int leddetect(void)
{
	ledcnt = 0;
	
	for(int i = 1; i<=MAX_LEDS; i++)
	{
		packet cmd;
		
		cmd.cmd = CMD_REPORT_ID;
		cmd.id = i;
		cmd.chksum = 0;
		
		transmitpacket(cmd);
		
		if(receivepacket() == i)
			leds[ledcnt++] = i;
	}
	
	return ledcnt;
}

int ledcount(void)
{
	return ledcnt;
}

void setcolor(int led, rgb color)
{
	packet cmd;
	
	cmd.cmd = CMD_SET_ABS;
	cmd.id = leds[led];
	cmd.rgb_color = color;
	cmd.val = 0;
	cmd.chksum = 0;
	
	transmitpacket(cmd);
}

rgb mulcolor(rgb base, unsigned char val)
{
	base.r = ((unsigned short)base.r*255)/val;
	base.g = ((unsigned short)base.g*255)/val;
	base.b = ((unsigned short)base.b*255)/val;
	
	return base;
}

rgb lerpcolor(rgb a, rgb b, unsigned char val)
{
	b.r = (((unsigned short)(b.r-a.r))*255)/val+a.r;
	b.g = (((unsigned short)(b.g-a.g))*255)/val+a.g;
	b.b = (((unsigned short)(b.b-a.b))*255)/val+a.b;
	
	return b;
}

rgb black = {0,0,0};
rgb white = {255,255,255};
